package com.miaowang.salad.jobqueue;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.net.OkHttpUtils;
import com.miaowang.salad.net.UrlUtils;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;
import com.path.android.jobqueue.RetryConstraint;

import de.greenrobot.event.EventBus;

/**
 * 获取个人信息
 * @author weihe
 *
 */
public class GetMineInfoJob  extends Job{

	private int flag = 0;//0 登录请求 1我的信息请求
	public GetMineInfoJob(int flag) {
		super(new Params(JobQueuePriority.PRIORITY_HIGH).groupBy(JobQueuePriority.JOBS_GROUP_USER).requireNetwork());
		this.flag = flag;
	}

	@Override
	public void onAdded() {
		
	}

	@Override
	protected void onCancel() {
		
	}

	@Override
	public void onRun() throws Throwable {
		 String result = OkHttpUtils.get(UrlUtils.GET_MINE_INFO);
		 OkHttpResponseData msg = JSON.parseObject(result, OkHttpResponseData.class);
		 if(flag == 0 ){
			 EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_MY_INFO_BEGIN,msg));
		 }else{
			 EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_MY_INFO,msg));
		 }
	} 

	@Override
	protected RetryConstraint shouldReRunOnThrowable(Throwable throwable,
			int runCount, int maxRunCount) {
		EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.GET_MY_INFO));
		return super.shouldReRunOnThrowable(throwable, runCount, maxRunCount);
	}
	
	@Override
	protected boolean shouldReRunOnThrowable(Throwable throwable) {
		if(flag == 0 ){
			EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.GET_MY_INFO_BEGIN));
		 }else{
			 EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.GET_MY_INFO));
		 }
		return false;
	}
	
	

}
